import 'package:flutter/material.dart';

class GKBDialogArguments {
  final String? title;
  final String? content;

  GKBDialogArguments({
    this.title,
    this.content,
  });
}

class GKBDialog {
  //中间弹框
  static void show(
    BuildContext context, {
    String? title,
    String? content,
    TextAlign? textAlign,
    String? leftText,
    String? rightText,
    Color? leftBtnColor,
    Color? rightBtnColor,
    final VoidCallback? onCancel,
    final VoidCallback? onConfirm,
    bool hiddenCancel = false,
  }) {
    showDialog(
        context: context,
        barrierDismissible: false,
        routeSettings: RouteSettings(
            name: 'GKBDialog',
            arguments: GKBDialogArguments(title: title, content: content)),
        builder: (context) {
          return BaseDialog(
              title: title,
              content: content == null
                  ? null
                  : Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16.0, vertical: 8.0),
                      child: Text(
                        content,
                        style: const TextStyle(fontSize: 14.0),
                        textAlign: textAlign ?? TextAlign.center,
                      ),
                    ),
              leftText: leftText,
              rightText: rightText,
              leftBtnColor: leftBtnColor,
              rightBtnColor: rightBtnColor,
              onCancel: onCancel,
              onConfirm: onConfirm,
              hiddenCancel: hiddenCancel);
        });
  }
}

class BaseDialog extends StatelessWidget {
  const BaseDialog({
    Key? key,
    this.title,
    this.titleTextStyle,
    this.content,
    this.leftText,
    this.rightText,
    this.onCancel,
    this.onConfirm,
    this.hiddenCancel = false,
    this.leftBtnColor,
    this.rightBtnColor,
  }) : super(key: key);

  final String? title;
  final TextStyle? titleTextStyle;
  final Widget? content;
  final String? leftText;
  final String? rightText;
  final Color? leftBtnColor;
  final Color? rightBtnColor;
  final VoidCallback? onCancel;
  final VoidCallback? onConfirm;
  final bool hiddenCancel;

  @override
  Widget build(BuildContext context) {
    Widget dialogTitle = Offstage(
      offstage: title == null || title == '' ? true : false,
      child: Padding(
        padding: const EdgeInsets.only(bottom: 8.0, left: 8, right: 8),
        child: Text(
          title == null ? '' : title.toString(),
          style: titleTextStyle ??
              const TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
        ),
      ),
    );

    Widget bottomButton = Row(
      children: <Widget>[
        hiddenCancel
            ? Container()
            : _DialogButton(
                text: leftText ?? 'Cancel',
                textColor: leftBtnColor ?? const Color(0xFF999999),
                onPressed: () {
                  Navigator.pop(context);
                  if (onCancel != null) {
                    onCancel!();
                  }
                },
              ),
        const SizedBox(
          height: 48.0,
          width: 0.6,
          child: VerticalDivider(),
        ),
        _DialogButton(
          text: rightText ?? 'Yes',
          textColor: rightBtnColor ?? Theme.of(context).primaryColor,
          onPressed: () {
            Navigator.pop(context);
            if (onConfirm != null) {
              onConfirm!();
            }
          },
        ),
      ],
    );

    Widget body = Material(
      borderRadius: BorderRadius.circular(8.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          const SizedBox(height: 24),
          dialogTitle,
          content == null ? Container() : Flexible(child: content!),
          const SizedBox(height: 8),
          const Divider(height: 1),
          bottomButton,
        ],
      ),
    );

    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets +
          const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
      duration: const Duration(milliseconds: 20),
      curve: Curves.easeInCubic,
      child: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Center(
          child: SizedBox(
            width: 270.0,
            child: body,
          ),
        ),
      ),
    );
  }
}

class _DialogButton extends StatelessWidget {
  const _DialogButton({
    Key? key,
    this.text = '',
    this.textColor,
    this.onPressed,
  }) : super(key: key);

  final String text;
  final Color? textColor;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: SizedBox(
        height: 48.0,
        child: TextButton(
          child: Text(
            text,
            style: TextStyle(fontSize: 14.0, color: textColor),
          ),
          onPressed: onPressed,
        ),
      ),
    );
  }
}
